﻿using System;
using System.Net;
#if SILVERLIGHT
using AbstraX.ClientInterfaces;
#else
using AbstraX.ServerInterfaces;
#endif
using System.Reflection;
using AbstraX.XPathBuilder;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.ServiceModel.DomainServices.Server;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
using System.Linq.Expressions;
using SLControlProvider.Web.Entities;
using AssemblyProvider.Web.Entities;
using ControlProviders.ProviderInterfaces;
using AbstraX;
using AbstraX.Templates;
using AbstraX.AssemblyInterfaces;
using AbstraX.TypeMappings;
using AbstraX.Contracts;
using MvvmTreeView;

namespace SLControlProvider.Web.Entities
{
    [DataContract, DebuggerDisplay("{ DebugInfo }"), ClientCodeGeneration(typeof(AbstraXClientInterfaceGenerator))]
    public class SLPropertyElement : IControlElement
    {
        protected IPropertyElement property;
        protected IBase parent;
        protected IProviderEntityService providerEntityService;

        public SLPropertyElement()
        {
        }

        public SLPropertyElement(IPropertyElement property, IBase parent)
        {
            this.property = property;
            this.parent = parent;

            providerEntityService = ((IBase)parent).ProviderEntityService;
        }

        public IAbstraXExtension LoadExtension()
        {
            return null;
        }

        [Exclude]
        IProviderEntityService IBase.ProviderEntityService
        {
            get
            {
                return providerEntityService;
            }
        }

        [DataMember]
        public float ChildOrdinal
        {
            get
            {
                return property.ChildOrdinal;
            }
        }

        [DataMember]
        public string DebugInfo
        {
            get
            {
                return this.GetDebugInfo();
            }
        }

        [DataMember]
        public bool IsContainer
        {
            get 
            {
                return property.IsContainer;
            }
        }

        [Include, Association("Parent_BaseType", "ID", "ParentID")]
        public BaseType DataType
        {
            get
            {
                return new BaseType(property.DataType, this);
            }
        }

        public IEnumerable<IAttribute> Attributes
        {
            get 
            {
                return property.Attributes;
            }
        }

        public IEnumerable<IOperation> Operations
        {
            get 
            {
                return new List<IOperation>();
            }
        }

        public IEnumerable<IElement> ChildElements
        {
            get
            {
                return property.ChildElements;
            }
        }

        [DataMember, Key]
        public string ID
        {
            get 
            {
                return property.ID;
            }
        }

        [DataMember]
        public string ParentID
        {
            get 
            {
                return property.ParentID;
            }
        }

        [DataMember]
        public string Name
        {
            get 
            {
                return property.Name;
            }
        }

        [DataMember]
        public string DesignComments
        {
            get
            {
                return property.DesignComments;;
            }
        }

        [DataMember]
        public bool HasDocumentation
        {
            get
            {
                return property.HasDocumentation;
            }
        }

        [DataMember]
        public string DocumentationSummary
        {
            get
            {
                return property.DocumentationSummary;
            }
        }

        [DataMember]
        public string Documentation
        {
            get
            {
                return property.Documentation;
            }
        }

        [DataMember]
        public string ImageURL
        {
            get
            {
                return property.ImageURL;
            }
        }

        [DataMember]
        public IBase Parent
        {
            get
            {
                return property.Parent;
            }
        }

        [DataMember]
        public string FolderKeyPair
        {
            get
            {
                return property.FolderKeyPair;
            }

            set
            {
                property.FolderKeyPair = value;
            }
        }

        public void ExecuteWhere(string property, object value)
        {
            this.property.ExecuteWhere(property, value);
        }

        public void ExecuteWhere(System.Linq.Expressions.Expression expression)
        {
            property.ExecuteWhere(expression);
        }

        public void ExecuteWhere(XPathAxisElement element)
        {
            property.ExecuteWhere(element);
        }

        public void ClearPredicates()
        {
            property.ClearPredicates();
        }

        public IRoot Root
        {
            get
            {
                IBase baseObject = this;

                while (baseObject != null)
                {
                    baseObject = baseObject.Parent;

                    if (baseObject is IRoot)
                    {
                        return (IRoot)baseObject;
                    }
                }

                return null;
            }
        }

        [DataMember]
        public DefinitionKind Kind
        {
            get
            {
                return property.Kind;
            }
        }

        [DataMember]
        public bool HasChildren
        {
            get
            {
                return property.HasChildren;
            }
        }

        [DataMember, Include, Association("Parent_Facet", "ID", "ParentID")]
        public Facet[] Facets
        {
            get
            {
                return property.Facets;
            }
        }

        [Exclude]
        public ContainerType AllowableContainerTypes
        {
            get { throw new NotImplementedException(); }
        }

        [Exclude]
        public ConstructType AllowableConstructTypes
        {
            get { throw new NotImplementedException(); }
        }

        [Exclude]
        public ContainerType DefaultContainerType
        {
            get { throw new NotImplementedException(); }
        }

        [Exclude]
        public ConstructType DefaultConstructType
        {
            get { throw new NotImplementedException(); }
        }

        [DataMember]
        public Modifiers Modifiers
        {
            get 
            {
                return property.Modifiers;
            }
        }

        IQueryable IPathQueryable.ExecuteWhere(string property, object value)
        {
            throw new NotImplementedException();
        }

        IQueryable IPathQueryable.ExecuteWhere(Expression expression)
        {
            throw new NotImplementedException();
        }

        IQueryable IPathQueryable.ExecuteWhere(XPathAxisElement element)
        {
            throw new NotImplementedException();
        }
    }
}
